package middleware

import (
	"golang-master/photo-gallery/utils"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		session, _ := utils.Store.Get(c.Request, "photo-session")
		userID, ok := session.Values["user_id"]

		if !ok || userID == nil {
			c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
			c.Abort()
			return
		}
		log.Println("user_id:", userID)

		c.Set("user_id", userID)
		c.Next()
	}
}
